package com.duoec.dw.lineage.service.analyser.commend;

import com.duoec.dw.lineage.dto.SqoopCommendArgument;
import com.duoec.dw.lineage.exceptions.LineageAnalyseException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author xuwenzhen
 */
@Service
public class TableCommendLineAnalyser extends BaseCommendLineAnalyser {
    /**
     * 用于检查的起始字段串，如果命中后就会执行
     *
     * @return 起始字段串
     */
    @Override
    public String getStart() {
        return "table ";
    }

    /**
     * 分析
     *
     * @param importDto 血缘关系数据
     * @param line      需要处理的行
     */
    @Override
    public void analyse(SqoopCommendArgument importDto, String line) {
        String tableName = line.substring(getStart().length()).trim();
        if (StringUtils.isEmpty(tableName)) {
            throw new LineageAnalyseException("无法解析tableName: " + line);
        }
        int index = tableName.indexOf('.');
        if (index == -1) {
            importDto.setMySqlTable(tableName);
            return;
        }
        String database = tableName.substring(0, index);
        String table = tableName.substring(index + 1);
        importDto.setMySqlDatabase(database);
        importDto.setMySqlTable(table);
    }
}
